<template>
    <div class="manage">
        <el-dialog
            title="新增用户"
            :visible.sync="dialogVisible"
            width="50%"
            :before-close="handleClose">
            <!-- 用户的表单信息 -->
            <!-- :inline="true" 设置行内表单 -->
            <el-form ref="form" :rules="rules" :inline="true" :model="form" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择性别">
                    <el-option label="男" :value="1"></el-option>
                    <el-option label="女" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" prop="birth">
                    <el-date-picker
                        v-model="form.birth"
                        type="date"
                        placeholder="请选择日期"
                        value-format="yyyy-MM-DD">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="地址" prop="addr">
                    <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>
        <div class="manage-header">
            <el-button @click="add" type="primary">
                + 新增
            </el-button>
            <!-- userForm搜索框 -->
            <el-form :model="userForm" :inline="true">
                <el-form-item>
                    <el-input placeholder="请输入名称" v-model="userForm.name"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="goSubmit">搜索</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="common-tab">
            <el-table
                stripe
                height="90%"
                :data="tableData"
                style="width: 100%">
                <el-table-column
                    prop="name"
                    label="姓名">
                </el-table-column>
                <el-table-column
                    prop="sex"
                    label="性别">
                    <!-- 作用域插槽 slot-scope="scope" 在父组件中获取子组件的数据-->
                    <template slot-scope="scope">
                        <span>{{ scope.row.sex == 1 ? '男' : '女' }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="age"
                    label="年龄">
                </el-table-column>
                <el-table-column
                    prop="birth"
                    label="出生日期">
                </el-table-column>
                <el-table-column
                    prop="addr"
                    label="地址">
                </el-table-column>
                <el-table-column
                    prop="addr"
                    label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="edit(scope.row)" type="success">编辑</el-button>
                        <el-button size="mini" @click="del(scope.row)" type="danger">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="page">
                <el-pagination
                    layout="prev, pager, next"
                    :total="total"
                    @current-change="page">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { getUser, addUser, editUser, delUser } from '../api'
export default {
    data() {
        return {
            dialogVisible: false,
            form: {
                name: '',
                age: '',
                sex: '',
                birth: '',
                addr: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' }
                ],
                age: [
                    { required: true, message: '请输入年龄', trigger: 'blur' }
                ],
                sex: [
                    { required: true, message: '请选择性别', trigger: 'blur' }
                ],
                birth: [
                    { required: true, message: '请选择出生日期', trigger: 'blur' }
                ],
                addr: [
                    { required: true, message: '请输入地址', trigger: 'blur' }
                ]
            },
            tableData: [],
            state: 1, // 1 表示新增  0 表示编辑
            total: 0, // 当前的总条数
            pageData: {
                page: 1,
                limit: 10
            },
            userForm: {
                name: ''
            }
        }
    },
    methods: {
        // 提交用户表单
        submit(){
            this.$refs.form.validate((valid) => {
                // console.log(valid,'valid')
                if(valid){
                    // 后续对表单数据的处理
                    if(this.state === 1){
                        addUser(this.form).then(() => {
                            // 重新获取列表的数据接口
                            this.getList()
                        })
                    }else{
                        editUser(this.form).then(() => {
                            // 重新获取列表的数据接口
                            this.getList()
                        })
                    }
                    // console.log(this.form,'form')

                    // 清空表单的数据
                    this.$refs.form.resetFields()

                    // 关闭弹窗
                    this.dialogVisible = false
                }
            })
        },
        // 弹窗关闭的时候
        // resetFields 对该表单项进行重置，将其值重置为初始值并移除校验结果
        handleClose(){
            // console.log(this.$refs.form)
            this.$refs.form.resetFields()
            this.dialogVisible = false
        },
        cancel(){
            this.handleClose()
        },
        edit(row){
            this.state = 0
            this.dialogVisible = true
            // 注意：此时需要对当前行数据进行深拷贝，否则会出错
            this.form = JSON.parse(JSON.stringify(row))
        },

        del(row){
            this.$confirm('您确认要删除该用户吗？', '温馨提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delUser({id:row.id}).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    // 重新获取列表的数据接口
                    this.getList()
                })
                
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

        add(){
            this.state = 1
            this.dialogVisible = true
        },

        // 用来获取列表的数据的方法
        getList(){
            // 获取用户列表数据
            getUser({params: {...this.userForm, ...this.pageData}}).then(({ data }) => {
                console.log(data)
                this.tableData = data.list
                this.total = data.count || 0
            })
        },
        // 选择页码的回调函数
        page(value){
            console.log(value,'value')
            this.pageData.page = value
            // 重新获取列表的数据接口
            this.getList()
        },
        // 列表的查询
        goSubmit(){
            this.getList()
        }
    },
    mounted(){
        // 页面首次加载的时候获取数据
        this.getList()
    }
}
</script>

<style lang="less" scoped>
.manage{
    height: 90%;
    .manage-header{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .common-tab{
        position: relative;
        // calc 动态计算
        height: calc(100% - 62px);
        .page{
            position: absolute;
            bottom: 0;
            right: 20px;
        }
    }
}
</style>